<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .wrap {
      width: 1072px;
      height: 400px;
      margin: 50px auto;
    }

    .wrap img {
      float: left;
      border: 1px solid black;
      margin-right: 20px;
    }

    .top {
      float: left;
      width: 600px;
      height: 220px;
      border-bottom: 1px solid #bbb;
      font-family: 'Microsoft yahei';
    }

    .top p span {
      font-size: 14px;
      color: .666;
      border: 1px solid #999;
      display: inline-block;
      padding: 8px;
      cursor: pointer;
    }

    .top p span.on {
      border: 2px solid #f60;
      padding: 7px;
      background: url(../images/on.png) no-repeat right bottom;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <img src="../images/black.jpg" alt="">
    <div class="top">
      <p class="color">
        <label class="">颜色</label>
        <span class="on">黑色</span>
        <span class="">白色</span>
        <span class="">红色</span>
        <span class="">黄色</span>
        <span class="">紫色</span>
        <span class="">绿色</span>
      </p>
      <p class="rom">
        <label class="">内存</label>
        <span class="on">64GB</span>
        <span class="">128GB</span>
        <span class="">256GB</span>
      </p>
      <p class="banben">
        <label>版本</label>
        <span class="on">全网通</span>
        <span>移动版</span>
        <span>联通版</span>
        <span>电信版</span>
      </p>
    </div>

  </div>


  <script>
    const imgArr = ['../images/black.jpg','../images/white.jpg', '../images/red.jpg', '../images/yellow.jpg', '../images/purple.jpg', '../images/green.jpg'];
   let color=document.querySelectorAll('.color span')
   let rom=document.querySelectorAll('.rom span')
   let banben=document.querySelectorAll('.banben span')
    let img=document.querySelector('.wrap img')
   
//     - 获取DOM元素，querySelectorAll获取的是伪数组（颜色、内存、版本、左侧图片）
// - 遍历伪数组，给伪数组中的每一个DOM元素注册点击事件
// - 在事件执行函数中利用排他思想给点击的元素添加on类名
// - 选择颜色的事件执行函数中还需要将左侧图片替换成对应颜色的图片
     for(let i=0;i<color.length;i++){
      color[i].addEventListener('click',function(){
        document.querySelector('.color .on').classList.remove('on')
        this.classList.add('on')
        img.src=imgArr[i]
      })
     }
     for(let i=0;i<rom.length;i++){
      rom[i].addEventListener('click',function(){
        document.querySelector('.rom .on').classList.remove('on')
        this.classList.add('on')
      })
     }
     for(let i=0;i<banben.length;i++){
      banben[i].addEventListener('click',function(){
        document.querySelector('.banben .on').classList.remove('on')
        this.classList.add('on')
      })
     }
  </script>

</body>

</html>